//
//  PKPlayerView.m
//  PianKe
//
//  Created by lanou on 15/10/6.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKPlayerView.h"
#import <UIImageView+WebCache.h>
#import "PKPlayerManager.h"
@implementation PKPlayerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        _palyerShowimg=[[UIImageView alloc]initWithFrame:CGRectMake(40, 40, SCREEN_WIDTH-80, SCREEN_WIDTH-80)];
        
        _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH+20, SCREEN_WIDTH, 30)];
        _titleLabel.textAlignment=NSTextAlignmentCenter ;
        _colletionButton=[UIButton  buttonWithType:UIButtonTypeCustom];
        [_colletionButton setImage:[ UIImage imageNamed: @"collectionButton"] forState:UIControlStateNormal];
        _colletionButton.frame=CGRectMake(80, SCREEN_WIDTH+60, 10, 20);
        _collectionNumLabel=[[UILabel alloc]initWithFrame:CGRectMake(95, SCREEN_WIDTH+60, 40, 20)];
        _commentButton=[UIButton buttonWithType:UIButtonTypeCustom];
        
        _commentButton.frame=CGRectMake(250, SCREEN_WIDTH+60, 10, 20);
        [_commentButton setImage:[UIImage imageNamed:@"commentButton"] forState:UIControlStateNormal];
        _commentNumLabel=[[UILabel alloc]initWithFrame:CGRectMake(265, SCREEN_WIDTH+60, 40, 20)];
        _downloadButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _downloadButton.frame=CGRectMake(10, SCREEN_WIDTH+100, 20, 20) ;
        [_downloadButton setImage:[UIImage imageNamed:@"downloadimg" ]forState:UIControlStateNormal];
        _progressSlider=[[UISlider alloc]initWithFrame:CGRectMake(50, SCREEN_WIDTH+100, 240, 20)];
     
        [_progressSlider addTarget:self action:@selector(changePlayerProgress:) forControlEvents:UIControlEventTouchUpInside];
        UITapGestureRecognizer *tapRecognized=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seektoPlayerTime:)] ;
        [_progressSlider addGestureRecognizer:tapRecognized];
        _remainTimeLabel=[[UILabel alloc]initWithFrame:CGRectMake(310, SCREEN_WIDTH+100, 60, 30)];
        [self addSubview:_palyerShowimg];
        [self addSubview:_titleLabel];
        [self addSubview:_colletionButton];
        [self addSubview:_commentNumLabel];
        [self addSubview:_commentButton];
        [self addSubview:_collectionNumLabel];
        [self addSubview:_downloadButton];
//        [self addSubview:_downloadButton];
        [self addSubview:_progressSlider];
        [self addSubview:_remainTimeLabel];
        
        
    }
    return self;
}
-(void)seektoPlayerTime:(UITapGestureRecognizer *)tapRecognized
{
    CGPoint location= [tapRecognized locationInView:self.progressSlider];
    CGFloat x=location.x;
    CGFloat scale=x/self.progressSlider.frame.size.width;
    [[PKPlayerManager shareManager]seekToTime:scale *([PKPlayerManager shareManager].finishTime) ];
}

-(void)changePlayerProgress:(UISlider*)slider
{
    CGFloat scale=slider.value/slider.maximumValue;
    
    NSUInteger timer=([PKPlayerManager shareManager]).finishTime *scale;
    [[PKPlayerManager shareManager] seekToTime:timer];
    

}

-(void)setRadionDetailModel:(PKMainLessonModel *)radionDetailModel
{
    _radionDetailModel=radionDetailModel;
    [_palyerShowimg sd_setImageWithURL:[NSURL URLWithString:radionDetailModel.playInfo.imgUrl]];
    _titleLabel.text=radionDetailModel.playInfo.title;
 NSTimer *timer  =   [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(changeRetaintime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
   //    [PKPlayerManager shareManager]
    
    
    
}
-(void)changeRetaintime
{
    PKPlayerManager *playerManager=[PKPlayerManager shareManager];
    NSUInteger retainTime=playerManager.finishTime-playerManager.currentTime;
    NSUInteger a=retainTime/60/10;
    NSUInteger  b=retainTime/60%10;
    NSUInteger c=retainTime%60/10;
    NSUInteger d=retainTime%60%10;
    _remainTimeLabel.text=[NSString stringWithFormat:@"%lu%lu：%lu%lu", a, b, c, d];
 CGFloat scale =   (CGFloat)([PKPlayerManager  shareManager]).currentTime/(CGFloat)([PKPlayerManager shareManager]).finishTime ;
    NSLog(@"noerpsjy");

    NSLog(@"%f",scale);
    self.progressSlider.value=self.progressSlider.maximumValue *scale;
    NSLog(@"%f",self.progressSlider.value);

}
@end
